//
//  MILSelectedDeviceViewController.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/16.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "MILSelectedDeviceViewController.h"
#import "MILAlarmClockSettingViewController.h"
#import "MILAuthorizationManagerViewController.h"
#import "MILSenceModeViewController.h"
#import "SelectedDeviceTableViewCell.h"
#import "SelectedDeviceTableViewCell.h"

@interface MILSelectedDeviceViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 空数据提示 */
@property (nonatomic,strong) UILabel *emptyLabel;

@end

@implementation MILSelectedDeviceViewController
#pragma mark -- UI
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshBaseControlValue
{
    [self setNavigationBarTitle:@"选择设备"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SelectedDeviceTableViewCell" bundle:nil] forCellReuseIdentifier:@"SelectedDeviceTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.emptyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
    }];
    
    if (self.myDataSourceArray.count == 0) {
        self.emptyLabel.hidden = NO;
    } else {
        self.emptyLabel.hidden = YES;
    }
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.myDataSourceArray.count>0)
    {
        return self.myDataSourceArray.count;
    }
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
//    if (!cell)
//    {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
//    }
//    if (self.myDataSourceArray.count>0)
//    {
//        MILUserDeviceModel *model = self.myDataSourceArray[indexPath.section];
//        cell.backgroundColor = [UIColor whiteColor];
//        cell.imageView.image = [UIImage imageNamed:@"deng"];
//        cell.textLabel.text = model.name;
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    }
//    
//    return cell;
    SelectedDeviceTableViewCell *Cell=[tableView  dequeueReusableCellWithIdentifier:@"SelectedDeviceTableViewCell"];
    if (self.myDataSourceArray.count>0)
    {
        MILUserDeviceModel *model = self.myDataSourceArray[indexPath.section];
        //isopen
        if ([model.is_open isEqualToString:@"1"]) {
            Cell.myDeviceImageView.image = [UIImage imageNamed:@"liangdeng"];
        } else{
            Cell.myDeviceImageView.image = [UIImage imageNamed:@"deng"];
        }

        if ([model.is_online isEqualToString:@"1"]) {
            Cell.myWifiStatusImageView.image = [UIImage imageNamed:@"icon_wifi_online"];
        } else {
            Cell.myWifiStatusImageView.image = [UIImage imageNamed:@"icon_wifi_offline"];
        }
        
        Cell.myDeviceNameLabel.text = model.name;
//        [Cell.myDeviceNameLabel layoutIfNeeded];
        Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return Cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.jumpViewControllerFlagString isEqualToString:@"1"])
    {
        MILAlarmClockSettingViewController *milAlarmClockSettingVC = [[MILAlarmClockSettingViewController alloc]init];
        milAlarmClockSettingVC.myDeviceModel = self.myDataSourceArray[indexPath.section];
        [self.navigationController pushViewController:milAlarmClockSettingVC animated:YES];
    }
    else if ([self.jumpViewControllerFlagString isEqualToString:@"2"])
    {
        //  分享
        MILAuthorizationManagerViewController *milAuthorizationManagerVC = [[MILAuthorizationManagerViewController alloc]init];
        milAuthorizationManagerVC.myDeviceModel = self.myDataSourceArray[indexPath.section];
        [self.navigationController pushViewController:milAuthorizationManagerVC animated:YES];
    } else if ([self.jumpViewControllerFlagString isEqualToString:@"3"]) {
        
        // 场景模式
        MILSenceModeViewController *milSenceModeVC = [[MILSenceModeViewController alloc]init];
        MILUserDeviceModel *model = self.myDataSourceArray[indexPath.section];
        milSenceModeVC.deviceModel = model;
        [self.navigationController pushViewController:milSenceModeVC animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return   1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 1.5;
}

#pragma mark -- 懒加载
- (UILabel *)emptyLabel{
    
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc]init];
        _emptyLabel.text = @"暂无设备";
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.textColor = [UIColor colorWithRed:151/255.0 green:151/255.0 blue:151/255.0 alpha:1];
        [self.view addSubview:_emptyLabel];
    }
    return _emptyLabel;
}

@end
